package com.algo.iplibrary;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class LangCN {
	private String minProvincialLevelId = "000000000000000000000009";
	private String maxProvincialLevelId = "000000000000000000000042";
	
	private String langFile;
	private String storageSystem = "local";
	
	private Map<String, String> langCN = new HashMap<String, String>();
	
	public LangCN(String langCNFile, String storageSystemType) {
		this.langFile = langCNFile;
		if (storageSystemType.toLowerCase().equals("hdfs")){
			storageSystem = "hdfs";
		}
		try {
			init();
		} catch (NumberFormatException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

	}
	
	private void init() throws NumberFormatException, IOException {
		if (storageSystem.equals("local")) {
			initLangCNLocal();
		} else {
			initLangCNHDFS();
		}
			
	}
	
	public String getRegionName(String regionID) {
		if (regionID == null) {
			return "not known";
		}
		if(regionID.compareTo(maxProvincialLevelId)<= 0  && regionID.compareTo(minProvincialLevelId) >= 0) {
			return langCN.get(regionID);
		}  
		return "not known";
		
	}
	
	private void initLangCNLocal() throws NumberFormatException, IOException {
		BufferedReader br;
		br = new BufferedReader(new FileReader(langFile));
		String str;

		while ((str = br.readLine()) != null) {
			String[] items = str.split("\\,");
			if (items.length == 2) {
				langCN.put(items[0], items[1]);
			}
		}
		br.close();
	}
	
	private void initLangCNHDFS() throws NumberFormatException, IOException {
		FileSystem fs = FileSystem.get(URI.create(langFile),new Configuration());
		FSDataInputStream fin = fs.open(new Path(langFile));
		BufferedReader br = new BufferedReader(new InputStreamReader(fin));
		String str;

		while ((str = br.readLine()) != null) {
			String[] items = str.split("\\,");
			if (items.length == 2) {
				langCN.put(items[0], items[1]);
			}
		}
		br.close();
	}
	
    public static void main(String[] args) throws IOException {
        // IpLibrary ipl = new IpLibrary("/dmp/dicmanager/IPlib-Region-000000000000000000000008-top100-new-20131230105900", "hdfs");
        // IpRegion ir= new IpRegion("/dmp/dicmanager/Region-000000000000000000000008-top100-new", "hdfs");
        // LangCN lc = new LangCN("/dmp/dicmanager/Lang-CN-file", "hdfs");

        IpLibrary ipl = new IpLibrary("/home/hadoop/lvliang/resource/IPlib-Region-000000000000000000000008-top100-new-20131230105900", "local");
        IpRegion ir= new IpRegion("/home/hadoop/lvliang/resource/Region-000000000000000000000008-top100-new", "local");
        LangCN lc = new LangCN("/home/hadoop/lvliang/resource/Lang-CN-file", "local");
        
        // IpLibrary ipl = new IpLibrary(
        // 		"E:\\IPlib-Region-000000000000000000000008-top100-20110728171818", "local");
        // IpRegion ir= new IpRegion("E:\\Region-000000000000000000000008-top100-new", "local");
        // LangCN lc = new LangCN("E:\\Lang-CN-20130308173130", "local");
        System.out.println(ipl.getIPLocation("112.117.1.1"));
        System.out.println(lc.getRegionName(ir.getProvincialID(ipl.getIPLocation("112.117.1.1"))));
	}
}
